package com.yc.fav.web;

import com.yc.fav.Utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/7
 * Email haijunzhou@hnit.edu.cn
 */
@WebServlet("/pageInfo")
public class PageInfoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String addr = req.getParameter("addr");
        Document doc = Jsoup.parse(new URL(addr), 5000);

        Element title = doc.selectFirst("head>title");
        Element descr = doc.selectFirst("meta[name$='escription']");
        Element keywo = doc.selectFirst("meta[name$='eywords']");

        Map<String,Object> data = new HashMap<>();
        data.put("name", title.text());
        data.put("intro", descr.attr("content"));
        data.put("tags", keywo.attr("content"));
        Utils.print(resp, data);
    }
}
